package com.metaera.app.controller.presenter;

import com.metaera.app.controller.core.SimplePaginationPresenter;
import com.metaera.app.controller.view.core.INetworkRequestView;
import com.metaera.app.data.DataRepository;
import com.metaera.app.data.model.BasicListResponse;

import com.metaera.app.data.model.SpecialResponse;
import com.metaera.app.data.network.ApiParam;
import com.metaera.app.data.rx.SchedulersTransformer;

import java.util.List;

import io.reactivex.Observable;


public class HotTopicListPresenter extends SimplePaginationPresenter<SpecialResponse.Popular.ListBean> {

    private INetworkRequestView<List<SpecialResponse.Selected>> mINetworkRequestView;

    public void callback(INetworkRequestView<List<SpecialResponse.Selected>> iNetworkRequestView){
        mINetworkRequestView = iNetworkRequestView;
    }


    @Override
    protected Observable<BasicListResponse<SpecialResponse.Popular.ListBean>> execute() {
        return DataRepository.getInstance()
                .getSpecial(mPage, ApiParam.PAGE_SIZE)
                .compose(SchedulersTransformer.transformer())
                .map(response -> {
                    SpecialResponse.Popular popular = response.getData().getPopular();
                    BasicListResponse.PageDataEntity<SpecialResponse.Popular.ListBean> pageData = new BasicListResponse.PageDataEntity<>();
                    pageData.setTotal(popular.getTotal());
                    pageData.setPage_size(popular.getPageSize());
                    pageData.setPage(popular.getPageNum());
                    pageData.setItems(popular.getList()); // 热门专题

                    List<SpecialResponse.Selected> selected = response.getData().getSelected(); // 精选专题
                    mINetworkRequestView.successful(selected);

                    return new BasicListResponse<>(pageData, response.getCode(), response.getMessage());
                });
    }

}
